home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / dev / lang / sbp3_1e.lzh / MENU.PL < prev    next >
Text File  |  1991-10-31  |  3KB  |  81 lines

  1. /* From the book PROLOG PROGRAMMING IN DEPTH
  2.    by Michael A. Covington, Donald Nute, and Andre Vellino.
  3.    Copyright 1988 Scott, Foresman & Co.
  4.    Non-commercial distribution of this file is permitted. */
  5.  
  6. % get1(C)
  7. %  accepts a line of input and returns only the first character.
  8. %  A good way to obtain a 1-character response from the user
  9. %  in a Prolog with buffered input, such as Quintus or ALS.
  10.  
  11. get1(C) :-
  12.   get(C),
  13.   repeat,
  14.     get0(N),
  15.     (N=10 ; N=13),
  16.   !.
  17.  
  18.  
  19. /* MENU.PL */
  20.  
  21. /*************************************************************************
  22.  * menu(Menu,Result)                                                     *
  23.  *   Displays a menu of up to 9 items and returns the user's choice.     *
  24.  *   A menu is a list of items, each of the form item(Message,Value)     *
  25.  *   where Message (an atom, in single quotes) is the message to be      *
  26.  *   displayed and Value is the value to be returned in Result if the    *
  27.  *   user chooses this item.                                             *
  28.  *************************************************************************/
  29.  
  30. menu(Menu,Result) :- menu_display(Menu,49,Last),
  31.                      menu_choose(Menu,49,Last,Result),
  32.                      nl.
  33.  
  34.      /* Display all the messages and simultaneously count them.
  35.         The count starts at 49 (ASCII code for '1'). */
  36.  
  37. menu_display([],SoFar,Last) :- !,
  38.                                /* not an item, so don't use this number */
  39.                                Last is SoFar - 1.
  40.  
  41. menu_display([item(Message,_)|Tail],SoFar,Last) :-
  42.       put(32),
  43.       put(32),
  44.       put(SoFar),       /* appropriate digit */
  45.       put(32),          /* blank */
  46.       write(Message),
  47.       nl,
  48.       Next is SoFar + 1,
  49.       menu_display(Tail,Next,Last).
  50.  
  51.      /* Get the user's choice. If invalid, make him try again. */
  52.  
  53. menu_choose(Menu,First,Last,Result) :-
  54.       write('Your choice ('),
  55.       put(First),
  56.       write(' to '),
  57.       put(Last),
  58.       write('):  '),
  59.       get1(Char),
  60.       menu_choose_aux(Menu,First,Last,Result,Char).
  61.  
  62. menu_choose_aux(Menu,First,Last,Result,Char) :-
  63.       Char >= First,
  64.       Char =< Last,
  65.       !,
  66.       menu_select(Menu,First,Char,Result).
  67.  
  68. menu_choose_aux(Menu,First,Last,Result,_) :-
  69.       put(7),           /* beep */
  70.       put(13),          /* carriage return */
  71.       menu_choose(Menu,First,Last,Result).
  72.  
  73.       /* Find the appropriate item to return for Char */
  74.  
  75. menu_select([item(_,Result)|_],First,First,Result) :- !.
  76.  
  77. menu_select([_|Tail],First,Char,Result) :-
  78.      NewFirst is First+1,
  79.      menu_select(Tail,NewFirst,Char,Result).
  80.  
  81.